package com.Travit;

import java.io.IOException;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;

public class DistributorIntermediate extends Activity{

	ImageButton logout;
	Button cancelbutton;
	Button savebutton;
	String loggedid;
	private static String urlRequestValidateBoxBarcode = "http://project492.engr.sjsu.edu/travITvalidateProduct.php";
	
	public void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		Bundle extras = getIntent().getExtras();
		setContentView(R.layout.distributorintermediate);
		savebutton = (Button) findViewById(R.id.intermediatesave);
		cancelbutton = (Button) findViewById(R.id.cancelupdatestop);
		Log.v("gen", "should be working");
		cancelbutton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Log.v("cancel", "should go back");
				Intent newIntent = new Intent(v.getContext(), Distributor.class);
				newIntent.putExtra("venid", loggedid);
				startActivity(newIntent);
			}			
		});
		logout = (ImageButton) findViewById(R.id.logout);

		logout.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				//SHOW INFORMATION FROM BARCODE
				//initialize barcodeid
				Intent newIntent = new Intent(v.getContext(), TravITActivity.class);
				//newIntent.putExtra("venid", loggedid);
		        startActivity(newIntent);
			}			
		});

		savebutton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Log.v("goingtoexecute", "try");
				String boxBarCodeID = "1000";
				EditText inputintermediate = (EditText) findViewById(R.id.intermediate_text);
				String tempintermediate = inputintermediate.getText().toString();
				Log.v("textentered", ""+tempintermediate);
				HttpClient client = new DefaultHttpClient();
				
//						+ "?intermediatePosition="+tempintermediate
//						+ "&boxbarcodeID="+boxBarCodeID);
				try {
					HttpPost request = new HttpPost(urlRequestValidateBoxBarcode
							+ "?intermediatePosition=" + tempintermediate
							+ "&boxbarcodeID=" + boxBarCodeID);
					Log.v("trying", "before execution");
					List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
					nameValuePairs.add(new BasicNameValuePair("intermediatePosition", tempintermediate));
				    nameValuePairs.add(new BasicNameValuePair("boxbarcodeID", boxBarCodeID));
				    request.setEntity(new UrlEncodedFormEntity(nameValuePairs));

					HttpResponse response = client.execute(request);
					HttpEntity entity = response.getEntity();
					String responseResult = EntityUtils.toString(entity);
					Log.v("executing", "" + responseResult);
					if(responseResult.equalsIgnoreCase("Valid Box"))
					{
						Log.v("in if", "valid box");
						Intent newIntent = new Intent(v.getContext(), Distributor.class);
						newIntent.putExtra("venid", loggedid);
						startActivity(newIntent);
					}
					else
					{
						Log.v("in else", "valid box");
						Intent newIntent = new Intent(v.getContext(), Distributor.class);
						newIntent.putExtra("venid", loggedid);
						startActivity(newIntent);
					}
				} catch (ClientProtocolException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				}
				Intent newIntent = new Intent(v.getContext(), Distributor.class);
				newIntent.putExtra("venid", loggedid);
				startActivity(newIntent);
			}			
		});

	}
}
